package com.gl.saasservice.announcement.controller;

import com.gl.framework.web.response.Result;
import com.gl.saasservice.announcement.service.AnnouncementService;
import com.gl.saasservice.announcement.vo.AnnouncementVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 公告管理
 */
@Controller
@RequestMapping("/announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    /*
     *公告列表
     */
    @GetMapping(value = "/pages")
    @ResponseBody
    public Result pages(@RequestHeader String token,
                        @RequestParam(defaultValue = "0") Integer pageNumber,
                        @RequestParam(defaultValue = "10") Integer pageSize,
                        AnnouncementVO vo) {
        return announcementService.pages(token, pageNumber, pageSize, vo);
    }

    /**
     * 公告发布
     *
     * @param vo
     * @param file
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Result add(@RequestHeader String token, AnnouncementVO vo, @RequestParam(value = "file", required = false) MultipartFile file) {
        return announcementService.save(token, vo, file);
    }

    /**
     * 公告修改
     *
     * @param vo
     * @param file
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public Result update(@RequestHeader String token, AnnouncementVO vo, @RequestParam(value = "file", required = false) MultipartFile file) {
        return announcementService.save(token, vo, file);
    }

    /**
     * 富文本
     * 广告详情上传图片
     *
     * @param file
     * @return
     */
    @PostMapping(value = "upload")
    @ResponseBody
    public Map<String, Object> uploadDetailFile(@RequestParam(value = "file", required = false) MultipartFile file) {
        return announcementService.uploadDetailFile(file);
    }

    /**
     * 富文本图片删除
     *
     * @param paths
     */
    @PostMapping(value = "upload/del")
    @ResponseBody
    public void deleteDetailFile(String paths) {
        announcementService.deleteDetailFile(paths);
    }

    /*
     *公告删除
     */
    @RequestMapping(method = RequestMethod.DELETE)
    @ResponseBody
    public Result delete(@RequestHeader String token, @RequestParam(name = "id", required = false) Long id) {
        return announcementService.delete(token, id);
    }


}
